//
//  TYZCustomButton.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/9/30.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "TYZCustomButton.h"

@implementation TYZCustomButton

+ (instancetype)buttonWithImage:(UIImage *)image title:(NSString *)title imageMode:(TYZCustomButtonMode)imageModel {
    TYZCustomButton *button = [[TYZCustomButton alloc] init];
    [button setImage:image forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    button.imageModel = imageModel;
    return button;
}
- (void)setImageModel:(TYZCustomButtonMode)imageModel {
    _imageModel = imageModel;
    dispatch_async(dispatch_get_main_queue(), ^{
        CGFloat imageWidth = CGRectGetWidth(self.imageView.bounds);
        CGFloat imageHeight = CGRectGetHeight(self.imageView.bounds);
        CGFloat space = 1;
        CGSize size = [self.titleLabel.text getSizeWithFont:self.titleLabel.font];
        CGFloat titleHeight = size.height;
        CGFloat titleWidth = size.width;
        switch (imageModel) {
            case CustomButtonMode_Default:
                
                break;
            case CustomButtonMode_Right:
                [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -(imageWidth+space*2), 0, (imageWidth+space*2))];
                [self setImageEdgeInsets:UIEdgeInsetsMake(0, (titleWidth+space*2), 0, -(titleWidth+space*2))];
                break;
            case CustomButtonMode_Up:
                [self setImageEdgeInsets:UIEdgeInsetsMake(-(imageHeight*0.5 + space*0.5), titleWidth*0.5, imageHeight*0.5 + space*0.5, -titleWidth*0.5)];
                [self setTitleEdgeInsets:UIEdgeInsetsMake(titleHeight*0.5 + space*0.5, -imageWidth*0.5, -(titleHeight*0.5 + space*0.5), imageWidth*0.5)];
                break;
            case CustomButtonMode_Down:
                
                break;
            default:
                break;
        }
    });
}
@end
